package study.intconsulting.util;

import java.util.List;

import study.intconsulting.vo.PcInfo;
import study.intconsulting.vo.TestPcInfo;
import study.intconsulting.vo.TestPcinfoList;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.XmlFriendlyReplacer;

public class XstreamUtil {

    public static String convertToXml(Object source, Class type, String root) {
        String result;

        try {
    		XmlFriendlyReplacer replacer = new XmlFriendlyReplacer("__", "_");
    		XStream xStream =  new XStream(new DomDriver("UTF-8", replacer)){
    			protected boolean useXStream11XmlFriendlyMapper() {
    				return true;
    			}
    		};
    		xStream.alias(root, type);

            result = xStream.toXML(source);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return result;
    }

    public static Object convertToPojo(String xml, Class type, String root) {
    	Object obj;
        try {
    		XmlFriendlyReplacer replacer = new XmlFriendlyReplacer("__", "_");
    		XStream xStream =  new XStream(new DomDriver("UTF-8", replacer)){
    			protected boolean useXStream11XmlFriendlyMapper() {
    				return true;
    			}
    		};
    		xStream.alias(root, type);

    		obj = xStream.fromXML(xml);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return obj;
    }
    
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String xml1 = XstreamUtil.convertToXml(new PcInfo("", "", "who_0"), new PcInfo().getClass(), "message");
		System.out.println(xml1);
		PcInfo info = (PcInfo)XstreamUtil.convertToPojo(xml1, new PcInfo().getClass(), "message");
		System.out.println(info);
		
		////////////////////////////////////////////////////////////////////////////////////////////////////////
		
        String firstAttribute = "test";
        String secondAttribute = "테스트";

        TestPcinfoList testPcinfoList = new TestPcinfoList();
        testPcinfoList.setManagerId(1);
        testPcinfoList.getPcinfoList().add(new TestPcInfo(firstAttribute, "ip1", "name1", "who1"));
        testPcinfoList.getPcinfoList().add(new TestPcInfo(secondAttribute, "ip2", "name2", "who2"));

        String xml2 = XstreamUtil.convertToXml(testPcinfoList, new TestPcinfoList().getClass(), "message");
        System.out.println("xml2 : " + xml2);
        
        try{
        	TestPcinfoList resPcinfoList = (TestPcinfoList)XstreamUtil.convertToPojo(xml2, new TestPcinfoList().getClass(), "message");
        	List<TestPcInfo> infoList = resPcinfoList.getPcinfoList();
        	for(int i=0;i<infoList.size();i++){
        		TestPcInfo temp = infoList.get(i);
        		System.out.println("getIp : " + temp.getIp());
        		System.out.println("getName : " + temp.getName());
        		System.out.println("getWho : " + temp.getWho());
        	}
        }catch(Exception e){
        	e.printStackTrace();
        }
	}

}
